/** 
 * DFS.java
 * @author Lex
 * 2007-9-12 ����12:36:03
 */
package org.lex.utils.tree;

import java.util.Iterator;

import javax.swing.DefaultBoundedRangeModel;

import org.lex.utils.collections.ArrayStack;
import org.lex.utils.collections.Stack;


public class DFS extends DefaultBoundedRangeModel implements Iterator<Node> {
	private Stack<Node> stack = null;

	public DFS(Node root) {
		this.stack = new ArrayStack<Node>(2);
		this.stack.push(root);
		this.setMaximum(1);
	}

	public boolean hasNext() {
		return false == stack.isEmpty();
	}

	public Node next() {
		this.setValue(this.getValue() + 1);
		Node top = stack.pop();
		int childCount = top.getChildCount();
		if (0 == childCount)
			return top;
		else
			this.setMaximum(this.getMaximum() + childCount);
		for (int i = 0; i < childCount; i++)
			stack.push(top.getChildAt(i));
		return top;
	}

	public void remove() {
	}
}
